package com.wn.ziroombackend.controller;


import com.wn.ziroombackend.entity.Admin;
import com.wn.ziroombackend.service.AdminService;
import com.wn.ziroombackend.utils.JwtUtil;
import com.wn.ziroombackend.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public R login(@RequestBody Admin admin) {
        Admin loginAdmin = adminService.login(admin);
        System.out.println(loginAdmin);
        if (loginAdmin != null) {
            if (loginAdmin.getPassword().equals(admin.getPassword())) {
                System.out.println(22222222);
                String token = JwtUtil.createToken(admin.getUsername(), 100);
                System.out.println(token);
                System.out.println(111111111);
                return R.ok("ok").data("token", token);

            } else {
                return R.error("密码错误");
            }
        } else {
            return R.error("账号不存在");

        }
    }

    @GetMapping("/info")
    public R info(String token) {
        String account = JwtUtil.getAccount(token);
        Admin admin = new Admin();
        admin.setUsername(account);
        Admin info = adminService.login(admin);


        return R.ok("ok").data("name", info.getUsername()).data("avatar",info.getAvatar());
    }


    @RequestMapping("/logout")
    public R logout(){
        return R.ok("ok");


    }
}
